<?php
//判断是否正当访问
if(!defined('IN_DISCUZ') || empty($_G['formhash']) || empty($_G['uid']) || empty($_G['gp_tid']) || !submitcheck('applylistsubmit')) {
	exit('Access Denied');
}

require_once DISCUZ_ROOT.'./source/plugin/donkey_activity/donkey_activity.func.php';
//判断活动状态，访问权限
$activity = get_activity('stop', 'manager');

//判断必要的表单域，这里只有一个applyid
if(empty($_G['gp_applyidarray'])) {
	showmessage('activity_choice_applicant');
}

//获取表单参数，reason
$reason = intval($_G['gp_reason']);
if($reason==0){
	$reason=NULL;
}

//获取表单参数，applyid
$uidarray = array();
$ids = dimplode($_G['gp_applyidarray']);
$query = DB::query("SELECT a.uid,a.applyid FROM ".DB::table('donkey_activity_apply')." a RIGHT JOIN ".DB::table('common_member')." m USING(uid) WHERE a.tid='$_G[gp_tid]' AND verified = 1 AND a.applyid IN (".$ids.")");
while($uid = DB::fetch($query)) {
	$uidarray[$uid['applyid']] = $uid['uid'];
}

//再次判断必要的表单域，这里只有一个applyid
if(empty($uidarray)) {
	showmessage('activity_choice_applicant');
}

if($_G['gp_operation'] == 'coin') {	//派发金币
	if(empty($reason)) {
		showmessage('请填写派发金币数量');
	}
	foreach($uidarray as $uid) {
		notification_add($uid, 'activity', 'activity_notification', array('tid' => $_G['gp_tid'], 'subject' => $thread['subject'], 'msg' => '感谢参加这次活动，奖励给您'.$reason.'金币'));
		//TODO 更新用户缓存
		//DB::query("UPDATE ".DB::table('donkey_activity_apply')." SET extcredits2=extcredits2+$reason WHERE uid IN ('$uids')");
		//updatecreditbyaction('activity', $uid, '', '', 1, 1, $_G['gp_tid']);
		DB::query("UPDATE ".DB::table('common_member_count')." SET extcredits2=extcredits2+$reason WHERE uid='".$uid."'");
	}
	showmessage('派发金币成功', 'forum.php?mod=viewthread&tid='.$_G['gp_tid'], array(), array('showdialog' => 1, 'closetime' => true));
} elseif($_G['gp_operation'] == 'refuse') {	//管理员拒绝某人参加
	DB::query("UPDATE ".DB::table('donkey_activity_apply')." SET verified='4' WHERE tid='$_G[gp_tid]' AND applyid IN (".$ids.")", 'UNBUFFERED');
	foreach($uidarray as $uid) {
		notification_add($uid, 'activity', 'activity_replenish', array(
			'tid' => $_G['gp_tid'],
			'subject' => $thread['subject'],
			'reason' => '您主动放弃了这次活动，以后不要再放鸽子了哦！！',
		));
	}
	showmessage('activity_delete_completion', 'forum.php?mod=viewthread&tid='.$_G['gp_tid']);
} elseif($_G['gp_operation'] == 'cancel') {	//某人不参加，放鸽子了
	DB::query("UPDATE ".DB::table('donkey_activity_apply')." SET verified='3' WHERE tid='$_G[gp_tid]' AND applyid IN (".$ids.")", 'UNBUFFERED');
	foreach($uidarray as $uid) {
		notification_add($uid, 'activity', 'activity_replenish', array(
			'tid' => $_G['gp_tid'],
			'subject' => $thread['subject'],
			'reason' => '您不能参加这次活动，有问题请回帖留言或者给活动发起者私信',
		));
	}
	showmessage('activity_delete_completion', 'forum.php?mod=viewthread&tid='.$_G['gp_tid']);
} elseif($_G['gp_operation'] == 'divide') {	//分工
	$roleid=0;
	foreach($uidarray as $applyid=>$uid) {
		$roleid=$_G['gp_role_'.$applyid];
		DB::query("UPDATE ".DB::table('donkey_activity_apply')." SET roles='$roleid' WHERE tid='$_G[gp_tid]' AND uid='".$uid."' AND applyid IN (".$ids.")", 'UNBUFFERED');
		notification_add($uid, 'activity', $notification_lang, array(
			'tid' => $_G['gp_tid'],
			'subject' => $thread['subject'],
			'reason' => '您在本次活动中被安排了 【'.activity_role($_G['gp_role_'.$uid]).'】 角色',
		));
	}
	showmessage('activity_auditing_completion', 'forum.php?mod=viewthread&tid='.$_G['gp_tid']);
}
?>